﻿using AbpVnext_Order.Model;
using AbpVnext_Order.Repository;
using AbpVnext_Order_Contracts.Dto;
using AbpVnext_Order_Contracts.Iservice;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace AbpVnext_Order.Service
{
    /// <summary>
    /// 业务模块切换数据库
    /// </summary>
    [Dependency(ServiceLifetime.Transient)]
    //[DisableValidation]   //关闭自动Api控制器，dto验证
    public class OrderService : CrudAppService<Order,  //模型
                                                OrderDto,  //结果出餐
                                                OderPageResultDto, //获取列表返回参数dto
                                                int,  //主键
                                                OderPageDto,  //查询入参
                                                OrderCreateDto,  //创建入参dto
                                                OrderUpdateDto> //更新入参dto
                                               , IOrderService
    //开启自动API控制器验证 service验证 加这个特性之后
    //如果局部有校验异常或者自动和手动api控制器有异常，
    //不会进入到全局自定义验证
    //,IValidationEnabled 
    {
        public OrderService(IOrderRepository orderRepository) : base(orderRepository)
        {

        }

        public IObjectMapper objectMapper { get; set; }

        //public ProjectDbContext ProjectDbContext { get; set; }

        //public OrderDbContext OrderDbContext { get; set; }

        public IOrderRepository orderRepository { get; set; }


        //public async Task<OrderDto> CreateAsync(OrderDto input)
        //{
        //    //同MySQL实例跨库查询
        //    //var datax = (await Repository.GetDbContextAsync()).Database.SqlQueryRaw<OrderDto>("SELECT a.Id,a.OrderSn,a.TotalPrice from AspNetCore_Global.`Order` as a LEFT JOIN AspNetCore_Order.`Order` as b ON a.OrderSn = b.OrderSn").ToList();

        //    Order order = objectMapper.Map<OrderDto, Order>(input);
        //    Order order2 = await Repository.InsertAsync(order);
        //    OrderDto orderDto = objectMapper.Map<Order, OrderDto>(order2);
        //    return orderDto;
        //}

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<OderPageResultDto>> GetListAsync(OderPageDto input)
        {
            //return base.GetListAsync(input);
            IQueryable<Order> orders = await Repository.GetQueryableAsync();
            IQueryable<Order> orders1 = orders.WhereIf(!string.IsNullOrEmpty(input.OrderSn), x => x.OrderSn.Contains(input.OrderSn)).OrderBy(x => x.Id);
            List<Order> orders2 = orders1.Skip(0).Take(5).ToList();
            var count = orders2.Count();
            PagedResultDto<OderPageResultDto> pagedResultDto = new PagedResultDto<OderPageResultDto>();
            List<OderPageResultDto> oderPageResultDtos = objectMapper.Map<List<Order>, List<OderPageResultDto>>(orders2);
            pagedResultDto.Items = oderPageResultDtos;
            pagedResultDto.TotalCount = count;
            return pagedResultDto;
        }

        /// <summary>
        /// 关闭自动Api控制器，dto验证
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[DisableValidation]
        [LogEnable]
        public override async Task<OrderDto> CreateAsync(OrderCreateDto input)
        {
            Order order = objectMapper.Map<OrderCreateDto, Order>(input);
            Order order2 = await Repository.InsertAsync(order);
            OrderDto orderDto = objectMapper.Map<Order, OrderDto>(order2);
            //OrderDto orderDto = new OrderDto();
            return orderDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderCreateDto"></param>
        public void ProdutctImage(OrderCreateDto orderCreateDto)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderCreateDto"></param>
        public void InsertProdutctImagess(OrderCreateDto orderCreateDto)
        {

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="orderCreateDto"></param>
        /// <returns></returns>
        public async Task<OrderCreateResultDto> CreateOrderAsync(OrderCreateDto orderCreateDto)
        {
            Order order = objectMapper.Map<OrderCreateDto, Order>(orderCreateDto);
            Order order1 = await Repository.InsertAsync(order);
            OrderCreateResultDto orderCreateResultDto = objectMapper.Map<Order, OrderCreateResultDto>(order1);
            return orderCreateResultDto;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="orderUpdateDto"></param>
        /// <returns></returns>
        public async Task<OrderUpdateResultDto> OrderUpdateAsync(OrderUpdateDto orderUpdateDto)
        {
            Order order2 = await Repository.FindAsync(orderUpdateDto.Id);
            //Order order = objectMapper.Map<OrderUpdateDto, Order>(orderUpdateDto);
            order2.OrderSn = orderUpdateDto.OrderSn;
            Order order1 = await Repository.UpdateAsync(order2);
            OrderUpdateResultDto orderCreateResultDto = objectMapper.Map<Order, OrderUpdateResultDto>(order1);
            return orderCreateResultDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderCreateDto"></param>
        //public void InsertAsync(OrderCreateDto orderCreateDto)
        //{

        //}
    }
}
